package com.houseman.workFlow.processer.impl;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houseman.workFlow.bean.WorkFlowFolder;
import com.houseman.workFlow.processer.Processer;
import com.houseman.workFlow.service.FolderService;
import com.houseman.workFlow.service.impl.FolderServiceImpl;
import com.houseman.workFlow.utils.FolderToJson;

/**
 * 流程文件夹相关请求
 */
public class FolderProcesser extends Processer{

	//service
	FolderService service = new FolderServiceImpl();
	
	@Override
	public void proccess(HttpServletRequest request, HttpServletResponse response) {
		
		String flag = request.getParameter("folder");
		
		String json = null;
		try {
			//新建流程文件夹
			if("save".equals(flag)) {
				String name = request.getParameter("name");
				
				WorkFlowFolder folder = new WorkFlowFolder();
				folder.setName(name);
				
				service.saveFolder(folder);
			}else {
				List<WorkFlowFolder> fs = null;
				
				if("all".equals(flag)) { //文件夹及其下的流程名称
					fs = service.queryFoldersAndGraphs();
				}else {
					fs = service.queryFolders();
				}

				//对象-->JSON
				FolderToJson util = new FolderToJson();
				json = util.folderToJson(fs);
			}
			
			
			//数据处理后返回处理结果提示
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json");
			
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
